package top.malegod.controller;

import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import top.malegod.model.Comments;
import top.malegod.model.Order;
import top.malegod.model.User;
import top.malegod.service.OrderService;
import top.malegod.utils.ServerResponse;

/**
 * Create by IntelliJ IDEA
 *
 * @author: AbelEthan
 * @date-Time: 2018/5/23 13:40
 * @description:  订单的基本功能
 */
@Controller
@RequestMapping("/order")
public class OrderController {

  @Autowired
  private OrderService orderService;

  /**
   * 获取未付款订单信息
   * @param session
   * @return
   */
  @RequestMapping(value = "/getUnpaidOrder.do",method = RequestMethod.POST)
  @ResponseBody
  public ServerResponse getUnpaidOrder(HttpSession session){
    User user = (User) session.getAttribute("user");
    if (user == null){
      return ServerResponse.createError("用户未登录，请先登录");
    }
    ServerResponse<Order> response = orderService.getUnpaidOrder(user.getId());
    return response;
  }

  /**
   * 获取进行中订单信息
   * @param session
   * @return
   */
  @RequestMapping(value = "/getOngoingOrder.do",method = RequestMethod.POST)
  @ResponseBody
  public ServerResponse getOngoingOrder(HttpSession session){
    User user = (User) session.getAttribute("user");
    if (user == null){
      return ServerResponse.createError("用户未登录，请先登录");
    }
    ServerResponse<Order> response = orderService.getOngoingOrder(user.getId());
    return response;
  }

    /**
     * 获取未接订单信息
     * @param session
     * @return
     */
    @RequestMapping(value = "/getOpenOrder.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse getOpenOrder(HttpSession session){
        User user = (User) session.getAttribute("user");
        if (user == null){
            return ServerResponse.createError("用户未登录，请先登录");
        }
        ServerResponse<Order> response = orderService.getOpenOrder(user.getId());
        return response;
    }

  /**
   * 获取订单详细信息
   * @param id
   * @param session
   * @return
   */
  @RequestMapping(value = "/findSingle.do", method = RequestMethod.POST)
  @ResponseBody
  public ServerResponse findOrder(String id,HttpSession session){
    User user = (User) session.getAttribute("user");
    if (user == null){
      return ServerResponse.createError("用户未登录，请先登录");
    }
    ServerResponse<Order> response = orderService.findOrder(user.getId(), id);
    return response;
  }

  /**
   * 获取所有订单
   * @param session
   * @return
   */
  @RequestMapping(value = "/findAllOrder.do", method = RequestMethod.POST)
  @ResponseBody
  public ServerResponse findAllOrder(HttpSession session){
    User user = (User) session.getAttribute("user");
    if (user == null){
      return ServerResponse.createError("用户未登录，请先登录");
    }
    ServerResponse<Order> response = orderService.findAllOrder(user.getId());
    return response;
  }


  /**
   * 生成订单
   * @param order
   * @param session
   * @return
   */
  @RequestMapping(value = "/addOrder.do", method = RequestMethod.POST)
  @ResponseBody
  public ServerResponse addOrder(Order order, HttpSession session){
    User user = (User) session.getAttribute("user");
    if (user == null){
      return ServerResponse.createError("用户未登录，请先登录");
    }
    order.setUserId(user.getId());
    ServerResponse response = orderService.createOrder(order);
    return response;
  }

  /**
   * 取消订单
   * @param id
   * @param session
   * @return
   */
  @RequestMapping(value = "/cancelOrder.do", method = RequestMethod.POST)
  @ResponseBody
  public ServerResponse cancelOrder(String id,HttpSession session){
    User user = (User) session.getAttribute("user");
    if (user == null){
      return ServerResponse.createError("用户未登录，请先登录");
    }
    ServerResponse response = orderService.cancelOrder(id,user.getId());
    return response;
  }

  /**
   *  删除订单
   * @param id
   * @param session
   * @return
   */
  @RequestMapping(value = "/deleteOrder.do", method = RequestMethod.POST)
  @ResponseBody
  public ServerResponse deleteOrder(String id, HttpSession session){
    User user = (User) session.getAttribute("user");
    if (user == null){
      return ServerResponse.createError("用户未登录，请先登录");
    }
    ServerResponse response = orderService.deleteOrder(id);
    return response;
  }

  /**
   * 评论订单
   * @param comments
   * @param session
   * @return
   */
  @RequestMapping(value = "/commentsOrder.do", method = RequestMethod.POST)
  @ResponseBody
  public ServerResponse commentsOrder(Comments comments, HttpSession session){
    User user = (User) session.getAttribute("user");
    if (user == null){
      return ServerResponse.createError("用户未登录，请先登录");
    }
    comments.setUserId(user.getId());
    ServerResponse response = orderService.commentsOrder(comments);
    return response;
  }


}
